package org.cid54.bukkit.command;

import org.cid54.bukkit.command.util.PlayerCommand;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.event.player.PlayerChatEvent;
import org.cid54.bukkit.Cid54PlayerListener;

/**
 *
 * @author Cid54
 */
public class BuildCommand implements PlayerCommand {

    public boolean execute(String command, PlayerChatEvent event, Cid54PlayerListener listener) {

        String[] commands = command.split(" ");

        if (commands.length > 3) {

            int xDist = Integer.parseInt(commands[1]);
            int zDist = Integer.parseInt(commands[2]);
            int yDist = Integer.parseInt(commands[3]);

            int matId = Integer.parseInt(commands[4]);

            Material m = Material.getMaterial(matId);

            Location l = Cid54PlayerListener.cloneLocation(event.getPlayer().getLocation());

            World w = event.getPlayer().getWorld();

            if (xDist < 0) {

                l.setX(l.getX() + xDist);
                xDist *= -1;

            }

            if (zDist < 0) {

                l.setZ(l.getZ() + zDist);
                zDist *= -1;

            }

            if (yDist < 0) {

                l.setY(l.getY() + yDist);
                yDist *= -1;

            }

            for (int y = 0; y < yDist; y++) {

                for (int x = 0; x < xDist; x++) {

                    for (int z = 0; z < zDist; z++) {

                        w.getBlockAt(l).setType(m);

                        l.setZ(l.getZ() + 1D);

                    }

                    l.setZ(event.getPlayer().getLocation().getZ());

                    l.setX(l.getX() + 1D);

                }

                l.setX(event.getPlayer().getLocation().getX());

                l.setY(l.getY() + 1);

            }
            return true;
        } else {
            return false;
        }


    }

    public String getCommand() {
        return "build";
    }

    public String getHelpText() {
        return "build x-dist z-dist y-dist material-id";
    }
}
